package mlc.applet;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.GradientPaint;
import java.awt.Graphics2D;
import java.awt.Point;

import mlc.Constants;
import mlc.Utils;

public class Painter {

	private MLCApplet applet;
	
	public Painter(MLCApplet applet) {
		this.applet = applet;
	}
	
	public void paintGameStatus(Graphics2D g){
		g.setColor(Color.white);
		String status = "Speeds ("+Utils.format(applet.getPlayer().getVehicle().getSpeedX())+
		" | "+applet.getPlayer().getVehicle().getSpeedY()+") "+
		"Position ("+Utils.format(applet.getPlayer().getVehicle().getGlobalPosition().getX())+
		" | "+Utils.format(applet.getPlayer().getVehicle().getGlobalPosition().getY())+")";
		g.drawString(status, 30, 30);
	}
	
	public void paintCrossOfPositionAndSpeed(Graphics2D g){
		int centerX = (int)applet.getPlayer().getGlobalPosition().getX();
    	int centerY = (int)applet.getPlayer().getGlobalPosition().getY();
    	int rangeWidth = 100;
    	int rangeHeight = 100;
    	int startX = centerX - rangeWidth/2;
    	int endX = centerX + rangeWidth/2;    	
    	int startY = centerY -rangeHeight/2;
    	int endY = centerY + rangeHeight/2;
    	double speedXPercentage = applet.getPlayer().getVehicle().getSpeedX() / applet.getPlayer().getVehicle().getEngine().getSpeedLimit();
    	double speedYPercentage = applet.getPlayer().getVehicle().getSpeedY() / applet.getPlayer().getVehicle().getEngine().getSpeedLimit();
    	//White cross
    	g.setStroke(new BasicStroke(8));
    	g.setColor(Color.white);
    	g.drawLine(startX, centerY, endX, centerY);
    	g.drawLine(centerX, startY, centerX, endY);
    	//Speeds
    	g.setColor(Color.green);
    	g.drawLine(centerX, centerY, (int)(centerX + (speedXPercentage * rangeWidth/2)), centerY);
    	g.drawLine(centerX, centerY, centerX, (int)(centerY + (speedYPercentage * rangeHeight/2)));
	}

	public void paintBackground(Graphics2D g2d) {
		GradientPaint gradientPaint = new GradientPaint(
			new Point(applet.getWidth()/2,applet.getHeight()), Color.blue,
			new Point(applet.getWidth()/2,0),Color.black);
		g2d.setPaint(gradientPaint);
		g2d.fillRect(0, 0, applet.getWidth(), applet.getHeight());
		
	}

	public void paintVehicleAsSquare(Graphics2D g2d) {
		int size = Constants.squarePixelsSize;
		g2d.setColor(Color.green);
		g2d.fillRect(
			(int)(applet.getPlayer().getGlobalPosition().getX() - size/2),
			(int)(applet.getPlayer().getGlobalPosition().getY() - size),
			size, size);
		
	}
}
